package com.itheima.Controller;

import com.alibaba.fastjson.JSON;
import com.itheima.domain.Book;
import com.itheima.domain.Chapter;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import sun.reflect.generics.tree.Tree;
import test.MyThread;
import test.PaChu;

import java.io.*;
import java.util.*;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author ryh
 * @date 2020-09-12 12:45
 */
public class DemoJsoup {
    static Scanner sc = new Scanner(System.in);
    static Chapter cp = new Chapter();

    public static void main(String[] args) throws IOException {
       /* PaChu.getBook();
        Thread thread = new Thread(new MyThread());
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(5, 10, 2, TimeUnit.SECONDS, new ArrayBlockingQueue<>(10), Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());
        for (int i = 0; i < 10; i++) {
            threadPoolExecutor.submit(thread);
        }
        threadPoolExecutor.shutdown();*/
        User user = new User();
        while (true) {
            System.out.println("输入1注册用户   输入2登录用户   输入3修改密码   输入exit退出系统");
            String choice1 = sc.next();
            switch (choice1) {
                case "1":
                    System.out.println("注册");
                    user.add();
                    break;
                case "2":
                    boolean flag = user.Login();
                    if (flag) {
                        while (true) {
                            System.out.println("欢迎来到黑马阅读器");
                            System.out.println("1.查看小说信息.");
                            System.out.println("2.查看小说分类.");
                            System.out.println("3.进入书架系统");
                            System.out.println("4.输入书籍名称来查找书籍");
                            System.out.println("5.输入作者名称来查找其所有小说");
                            System.out.println("exit:退出");
                            String choice = sc.next();
                            switch (choice) {
                                case "1":
                                    System.out.println("查看小说信息");
                                    //findAllBookName();
                                    //查看书本名字
                                    findOneTest();
                                    //查看文本内容
                                    break;
                                case "2":
                                    System.out.println("查看小说分类");
                                    findAllBookType();
                                    //查看书本分类
                                    break;
                                case "3":
                                    //System.out.println("书架");
                                    List<Book> allBookName = findAllBookName();
                                    BookRack bookRack = new BookRack();
                                    bookRack.start(allBookName);
                                    break;
                                case "4":
                                    while (true) {
                                        System.out.println("输入书籍名称来查找书籍(输入exit返回上一级)");
                                        FindBookName findBookName = new FindBookName();
                                        String name = findBookName.findName(findAllBookName());
                                        if(name == null){
                                            break;
                                        }
                                        StringBuilder sb = DemoJsoup.getZhangJieString();
                                        String shu = null;
                                        String s = sb.toString();
                                        List<Book> chapters = JSON.parseArray(s, Book.class);
                                        for (Book book : chapters) {
                                            if (book.getBookId().equals(name)) {
                                                shu = book.getBookTitle();
                                            }
                                        }
                                        DemoJsoup.findOneBook(chapters, shu);
                                    }

                /*
                        解析Json获得书籍名称和用户输入做判断
                     */
                                    break;
                                case "5":
                                    System.out.println("输入作者名称来查找其所有小说");
                                    FindBookAuthor findBookAuthor = new FindBookAuthor();
                                    String author = findBookAuthor.findAuthor(findAllBookName());
                                    StringBuilder sb3 = DemoJsoup.getZhangJieString();
                                    String shu2 = null;
                                    String s2 = sb3.toString();
                                    List<Book> chapters2 = JSON.parseArray(s2, Book.class);
                                    for (Book book : chapters2) {
                                        if (book.getBookAuthor().equals(author)) {
                                            shu2 = book.getBookTitle();
                                        }
                                    }
                                    List<Book> list = findAllBookName();
                                    ArrayList<Book> books = new ArrayList<>();
                                    for (Book book : list) {
                                        if (book.getBookAuthor().equals(author)) {
                                            books.add(book);
                                        }
                                    }
                                    for (Book book : books) {
                                        System.out.println(book.getBookTitle() + " " + book.getBookId());
                                    }
                                    while (true) {
                                        System.out.println("请输入您要查看的小说id(输入exit返回上一级)");
                                        String id = sc.next();
                                        if("exit".equals(id)){
                                            break;
                                        }
                                        findOneBook(chapters2, id);
                                    }
                                    break;
                                case "exit":
                                    System.out.println("退出黑马阅读器");
                                    System.exit(0);
                                default:
                                    System.out.println("输入有误,请重新输入");
                            }
                        }
                    } else {
                        break;
                    }
                case "3":
                    user.modify();
                    break;
                case "exit":
                    System.out.println("感谢您的使用");
                    System.exit(0);
                default:
                    System.out.println("输入错误请重新输入");
            }

        }
    }


    private static void findOneTest() throws IOException {
        //根据编号查看某一本小说
        //查看书本名称
        StringBuilder sb = getZhangJieString();
        lo:
        while (true) {
            String s = sb.toString();
            List<Book> chapters = JSON.parseArray(s, Book.class);
            for (Book book : chapters) {
                System.out.println(book.getBookTitle() + ":  " + book.getBookId());
                //获取所有的小说编号和小说名称
            }


            System.out.println("输入exit退出");
            System.out.println("输入您想要查看的小说编号");
            String choice = sc.next();
            if (choice.equals("exit")) {
                return;
            }
            findOneBook(chapters, choice);
        }
    }

    public static StringBuilder getZhangJieString() throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("reptileDemozcy/books/book.txt"));
        StringBuilder sb = new StringBuilder();
        String a;
        while ((a = br.readLine()) != null) {
            sb.append(a);
        }
        br.close();
        return sb;
    }

    public static void findOneBook(List<Book> chapters, String choice) throws IOException {
        int i = 1;
        for (Book chapter : chapters) {
            if (chapter.getBookTitle().equals(choice)) {
                i = -1;
            }
        }
        if (i == 1) {
            System.out.println("您输入的数字有误");
            return;
        }
        for (Book chapter : chapters) {
            if (chapter.getBookTitle().equals(choice)) {
                for (Book book : chapters) {
                    System.out.println("书名:" + book.getBookTitle());
                    System.out.println("作者:" + book.getBookAuthor());
                    System.out.println("书号:" + book.getBookId());
                    System.out.println(book.getReadCount());
                    System.out.println("小说类型:" + book.getCategory());
                    System.out.println("小说状态:" + book.getState());
                    System.out.println("小说字数:" + book.getWordCount());
                    System.out.println("简介:" + book.getBookIntro());
                    System.out.println("按1进入章节选择");
                    System.out.println("按2返回上一级");
                    String choice2 = sc.next();
                    if (choice2.equals("1")) {
                        findAllsection(choice);
                    } else if (choice2.equals("2")) {
                        //输入2返回上一级
                    }
                }
            }
        }

    }

    private static void findAllsection(String choice) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("reptileDemozcy/books/ZhangJie.txt"));
        String s;
        List<Chapter> chapters = null;
        while ((s = br.readLine()) != null) {
            chapters = JSON.parseArray(s, Chapter.class);
        }
        br.close();
        TreeSet<Chapter> set = new TreeSet<>(new Comparator<Chapter>() {
            @Override
            public int compare(Chapter o1, Chapter o2) {
                return o1.getId() - o2.getId();
            }
        });
        for (int i = 0; i < chapters.size(); i++) {
            Chapter chapter = chapters.get(i);
            set.add(chapter);
        }
        for (Chapter chapter : set) {
            System.out.println(chapter.getId() + " " + chapter.getChapterTitle());
        }
        //打印所有章节
        System.out.println("输入章节编号打开对应章节(输入exit退出)");
        String choice1 = sc.next();
        if ("exit".equals(choice1)) {
            return;
        }
        int i = Integer.parseInt(choice1);
        if (i >= 1 && i <= set.size()) {
            findZj(choice, set, choice1);
        } else {
            System.out.println("没有对应章节");
            findOneTest();
        }
    }

    private static void findZj(String choice, TreeSet<Chapter> chapters, String choice1) throws IOException {
        if (choice1.equals("exit")) {
            return;
        }

        for (Chapter chapter : chapters) {
            Integer id = chapter.getId();
            String s1 = String.valueOf(id);
            if (s1.equals(choice1)) {
                System.out.println(chapter.getChapterTitle());
                File chapterFile = chapter.getChapterFile();
                BufferedReader br3 = new BufferedReader(new FileReader(chapterFile));
                String a;
                String[] splits = null;
                while ((a = br3.readLine()) != null) {
                    splits = a.split("。");
                }

                for (int i1 = 0; i1 < splits.length; i1++) {
                    try {
                        System.out.println(splits[i1]);
                    } catch (Exception e) {
                        System.out.println("没有上一页");
                        i1 = 0;
                    }
                    System.out.println();
                    if (((i1 + 1) % 10) == 0) {
                        System.out.println("输入exit返回上一级,输入其他继续观看");
                        System.out.println("输入a跳转至上一页");
                        String ch = sc.next();
                        if ("exit".equals(ch)) {
                            return;
                        } else if ("a".equals(ch)) {
                            i1 = i1 - 20;
                        }
                    }
                }
                while (true) {
                    System.out.println("输入w查看上一章节");
                    System.out.println("输入s查看下一章节");
                    String up = sc.next();
                    int i1 = Integer.parseInt(choice1);
                    if ("w".equals(up)) {
                        if ((i1 - 1) > 0 && (i1 - 1) <= chapters.size()) {
                            String s = String.valueOf(i1 - 1);
                            findZj(choice, chapters, s);
                            break;
                        } else {
                            System.out.println("没有上一章");
                            break;
                        }
                    } else if ("s".equals(up)) {
                        if ((i1 + 1) > 0 && (i1 + 1) <= chapters.size()) {
                            String s = String.valueOf(i1 + 1);
                            findZj(choice, chapters, s);
                            break;
                        } else {
                            System.out.println("没有最新章节,请给作者寄一些刀片");
                            break;
                        }
                    } else {
                        System.out.println("输入错误,请重新输入");
                    }
                }
            }
        }
    }


    private static List<Chapter> loadBookName() throws IOException {
        BufferedReader br2 = new BufferedReader(new FileReader("reptileDemozcy/src/books/zhangjie.txt"));
        StringBuilder sb2 = new StringBuilder();
        //从本地文件中获取小说目录
        String line;
        while ((line = br2.readLine()) != null) {
            sb2.append(line);
        }

        br2.close();
        String s1 = sb2.toString();
        //拼接字符串
        List<Chapter> chapters2 = JSON.parseArray(s1, Chapter.class);
        String number = sc.next();
        //Json小说目录转换成集合

        for (Chapter chapter : chapters2) {
            System.out.println(chapter);
        }
        return chapters2;
    }

    private static void findAllBookType() throws IOException {
        //查看书本类别
        BufferedReader br = new BufferedReader(new FileReader("reptileDemozcy/books/book.txt"));
        StringBuilder sb = new StringBuilder();
        String a;
        while ((a = br.readLine()) != null) {
            sb.append(a);
        }

        String s = sb.toString();
        List<Book> books = JSON.parseArray(s, Book.class);
        TreeSet<Book> books2 = new TreeSet<Book>(new Comparator<Book>() {
            @Override
            public int compare(Book o1, Book o2) {
                int i1 = Integer.parseInt(o1.getBookTitle());
                int i2 = Integer.parseInt(o2.getBookTitle());
                return i1 - i2;
            }
        });
        for (Book book : books) {
            books2.add(book);
        }
        for (Book book : books2) {
            System.out.println(book.getCategory());
        }
        while (true) {
            System.out.println("请输入您想查看的分类(输入exit返回上一级)");
            String choice = sc.next();
            switch (choice) {
                case "仙侠小说":
                    TreeMap<String, Book> treeMap = new TreeMap<String, Book>();
                    for (Book book : books) {
                        if (book.getCategory().equals(choice)) {
                            System.out.println(book.getBookTitle() + " " + book.getBookId());
                            treeMap.put(book.getBookTitle(), book);
                        }
                    }
                    System.out.println("请输入您想查看的书籍编号(输入exit返回上一级");
                    String id = sc.next();
                    Set<String> integers = treeMap.keySet();
                    if ("exit".equals(id)) {
                        break;
                    }
                    for (String integer : integers) {
                        if (integer.equals(id)) {
                            Book book = treeMap.get(integer);
                            String s1 = getZhangJieString().toString();
                            List<Chapter> chapters = JSON.parseArray(s1, Chapter.class);
                            for (Chapter chapter : chapters) {
                                findOneBook(books, id);
                            }
                        }
                    }
                    System.out.println("没有该编号的书");
                    break;
                case "exit":
                    return;
                default:
                    System.out.println("输入有误,请重新输入");
                    break;
            }
        }
    }

    private static List<Book> findAllBookName() throws IOException {
        //查看书本名称
        BufferedReader br = new BufferedReader(new FileReader("reptileDemozcy/books/book.txt"));
        StringBuilder sb = new StringBuilder();
        String a;
        while ((a = br.readLine()) != null) {
            sb.append(a);
        }

        String s = sb.toString();
        List<Book> books = JSON.parseArray(s, Book.class);
        /*for (Book book : books) {
            System.out.println(book.getBookId() + ":" + book.getBookTitle());
        }*/
        br.close();
        return books;
    }
}
